package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.mysql.jdbc.PreparedStatement;

import dto.HintsDto;
import dto.UserDto;

public class CopyOfUserDao {

	public String insertHint(HintsDto hint) {

		if (hint == null)
			return "Insert failed! Input data is null.";
		String response = "";

		Connection connection = DataProvider.getConnection();
		String sql = "INSERT INTO hints (hint_id, hint_desc) VALUES (?,?)";

		PreparedStatement preparedStatement;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(sql);
			preparedStatement.setString(1, hint.getHintId());
			preparedStatement.setString(2, hint.getHintDesc());

			preparedStatement.execute();
			response = "Inserted successfully!";

			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
			response = "Insert failed! " + e.getMessage();
		}

		return response;
	}

	public String deleteHint(String hintId) {

		String response = "";

		Connection connection = DataProvider.getConnection();
		String sql = "DELETE FROM hints WHERE hint_id = ?";

		PreparedStatement preparedStatement;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(sql);
			preparedStatement.setString(1, hintId);

			preparedStatement.execute();
			response = "Deleted successfully!";

			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
			response = "Delete failed! " + e.getMessage();
		}

		return response;
	}

	public String updateHint(HintsDto hint) {

		String response = "";

		Connection connection = DataProvider.getConnection();
		String sql = "UPDATE hints SET hint_desc = ? WHERE hint_id = ?";

		PreparedStatement preparedStatement;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(sql);

			preparedStatement.setString(1, hint.getHintDesc());

			preparedStatement.execute();
			response = "Updated successfully!";

			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
			response = "Update failed! " + e.getMessage();
		}

		return response;
	}

	public HintsDto getHintById(String hintId) {
		HintsDto hint = null;

		Connection connection = DataProvider.getConnection();
		String sql = "SELECT * FROM hints WHERE hint_id = ?";

		PreparedStatement preparedStatement;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(sql);
			preparedStatement.setString(1, hintId);
			ResultSet resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				hint = new HintsDto();
				hint.setHintId(resultSet.getString("hint_id"));
				hint.setHintDesc(resultSet.getString("hint_desc"));
			}

			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return hint;
	}

	public ArrayList<HintsDto> getAllHint() {

		ArrayList<HintsDto> listHint = new ArrayList<>();

		Connection connection = DataProvider.getConnection();
		String sql = "SELECT * FROM hints";

		Statement statement;
		try {
			statement = connection.createStatement();

			ResultSet resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				HintsDto hint = new HintsDto();
				hint.setHintId(resultSet.getString("hint_id"));
				hint.setHintDesc(resultSet.getString("hint_desc"));
				listHint.add(hint);
			}

			statement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return listHint;
	}

	public String insertUser(UserDto userDto) {
		// TODO Auto-generated method stub
		return null;
	}

}
